<template>
    <div class="container">
        <div class="box">
            <div class="left">
                <div id="face" class="paused">
                    <img src="https://ts1.cn.mm.bing.net/th/id/R-C.b42f14a412328f55cb5e8c4eb33474b8?rik=x%2fDNTh%2fa5jBNdw&riu=http%3a%2f%2fpic.baike.soso.com%2fp%2f20090110%2f20090110120000-101230.jpg&ehk=qFL47TfyKoi7YElvoELPxJEDsri%2fMCd5EjuxL1DTXms%3d&risl=&pid=ImgRaw&r=0"
                        alt="">
                </div>
                <div style="margin-left: 5px;font-family: '行书';">一程山水(毛不易)</div>
            </div>
            <div class="right">
                <div class="play_btn" @click="playStatus">
                    <van-icon v-if="!player" name="play-circle-o" size="28px" />
                    <van-icon v-else name="pause-circle-o" size="28px" />
                </div>
                <div class="list" style="margin-left: 5px;">
                    <van-icon name="bars" size="28px" />
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue';
const player = ref(false);
const toggle = ref(false);
let audio = ref(null);
const audioUrl = 'http://123.57.81.149/1.mp3'
const playStatus = () => {
    // player.value = !player.value;
    // togglePlay();
    player.value = !player.value; // 切换播放状态
    // 首次点击时初始化音频对象（避免重复创建）
    if (!audio.value) {
        audio.value = new Audio(audioUrl);
        // 监听音频播放结束（自动停止）
        audio.value.addEventListener('ended', () => {
            player.value = false; // 播放结束后重置状态
        });
    }
    // 控制音频播放/暂停
    if (player.value) {
        // 播放音频（必须在用户交互中触发，浏览器允许）
        audio.value.play();
    } else {
        // 暂停音频
        audio.value.pause();
    }

    // 同步你的原始 toggle 状态（可选，根据你的需求保留）
    // toggle.value = !toggle.value;
    togglePlay();

}
const togglePlay = () => {
    toggle.value = !toggle.value;
    if (toggle.value) {
        document.getElementById('face').classList.remove('paused');
    } else {
        document.getElementById('face').classList.add('paused')
    }
    
}
</script>
<style scoped lang="less">
@keyframes rotate {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.container {
    width: 100%;
    height: 60px;
    background-color: #fff;
    border-top: 1px solid #ccc;
    position: fixed;
    padding: 10px;
    box-sizing: border-box;
    bottom: 0;

    .box {
        display: flex;
        justify-content: space-between;

        .left {
            display: flex;
            align-items: center;

            #face {
                animation: rotate 2s linear infinite;
            }

            #face.paused {
                animation-play-state: paused;
            }

            #face img {
                width: 40px;
                height: 40px;
                border-radius: 50%;
            }
        }

        .right {
            display: flex;
            align-items: center;
        }
    }
}
</style>